
/**
 * Created by Administrator on 2015/3/6.
 */

var SaleReprot = (function(){
	var Option = null;
	var myChart = null;
	var curLid = "";
	var curStartTime = "";
	var curTheme = "infographic"; //infographic macarons
	var chartContainer = null;
	var getLid = function(){
		return $("#jqSelect").val();
	};
	var getStartTime = function(){
		return $("#saleReprotMod .dateTrigger[checked=checked]").val();
	};
	var initChart = function(){
		var container = document.getElementById("echartContainerCon");
		var lid = getLid();
		var startTime = getStartTime();
		myChart = echarts.init(container,curTheme);
		if(lid && startTime){
			query(lid,startTime);
		}
	};
	var query = function(lid,startTime){
		lid = lid || curLid;
		startTime = startTime || curStartTime;
		if(!lid){
			console && console.log("缺省lid");
			return false;
		}
		if(!startTime){
			console && console.log("缺省startTime");
			return false;
		}
		PFT_GLOBAL.G.Ajax({
			url : "http://www.12301.cc/xz/getOrder.php",
			data : { "lid":lid, "btime3":startTime },
			dataType : "json",
			type :'GET',
			loading : function(){
				myChart.showLoading();
			},
			removeLoading : function(data){
				myChart.hideLoading();
			},
			timeout : function(){console && console.log("获取数据超时")},
			serverError : function(){console && console.log("接口出错www.12301.cc/xz/getOrder.php")}
		},function(res){
			Option = res;
			alert(Option);
			buildChart();
		});
	};
	var buildChart = function(opt,theme){
		if(!opt){
			opt = Option;
		}else{
			Option = opt;
		}
		if(!theme){
			theme = curTheme;
		}else{
			curTheme = theme;
		}
		if(!opt || !theme){
			console && console.log("buildChart缺省opt或theme");
			return false;
		}
		var container = document.getElementById("echartContainerCon");
		myChart.hideLoading();
		if (myChart && myChart.dispose) {
			myChart.dispose();
		}
		myChart = echarts.init(container, theme);
		myChart.setTheme(theme);
		myChart.setOption(opt, true);
	};


	function init(){
		$("#jqSelect").select2({
			placeholder: "Select a State",
			allowClear: true
		});
		$("#queryChartBtn").on("click",function(e){
			var lid = getLid();
			var startTime = getStartTime();
			if(!lid){
				console && console.log("缺省lid");
				return false;
			}
			if(!startTime){
				console && console.log("缺省startTime");
				return false;
			}
			query(lid,startTime);
		})
		$("#refreshChartBtn").on("click",function(e){
			if(!Option || !curTheme) return false;
			myChart.showLoading();
			setTimeout(function(){
				buildChart();
			},500)
		})
		$("#saleReprotMod").on("click","input.dateTrigger",function(e){
			var target = $(e.currentTarget);
			target.siblings().removeAttr("checked");
			target.attr("checked",true);
		})
		$("#saleReprotMod .dateTrigger:first").trigger("click");
		initChart();
	}


	init()
})();